package com.lsa.letcode.the14.longestCommon;

public class Solution {
	public String longestCommonPrefix(String[] strs) {
		if (strs.length == 1) {
			return strs[0];
		}
		else if (strs.length == 0) {
			return "";
		}
		
		int prefixLength = strs[0].length();
		String prefix = strs[0];
		for (int i = 1; i < strs.length; i++) {
			String s = strs[i];
			if (s.length() == 0) {
				prefixLength = 0;
				break;
			}
			for (int j = 0; j < prefixLength && j < s.length(); j++) {
				if (prefix.charAt(j) != s.charAt(j)) {
					prefixLength = j;
					break;
				}
			}
			if (prefixLength > s.length()) {
				prefixLength = s.length();
			}
		}
		
		return prefix.substring(0, prefixLength);
	}
}